package com.eascs.loansettled.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.bankloan.dao.O2oBankLoanDao;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.loansettled.entity.O2oLoanSettledFormEntity;

@Service
public class O2oSettledRecordService {

	@Autowired
	O2oBankLoanDao loanapplyDao;

	/**
	 * 分页查询结清操作记录
	 * 
	 * @return
	 */
	public PageResult<Object[]> findByPage_JQ(O2oLoanSettledFormEntity entity,String opType) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 100, entity.getO2oIndex());
		StringBuffer querySql = new StringBuffer();
		
		querySql.append("  SELECT ol.loanapply_no,lol.bill_code,lol.op_psn,lol.op_date,lol.settle_date  ");
		querySql.append("  FROM o2o_record_log lol  ");
		querySql.append("  LEFT JOIN o2o_loanapply ol ON lol.bill_code=ol.bill_code  ");
		querySql.append("  WHERE /**DATA_PERMISSION(ol.ltd)**/ 1=1  ");
		
		if (!StringUtil.isEmpty(opType)) {
			querySql.append(" AND lol.op_type = ?");
			sqlPageQuery.addParam(opType);
		}
		
		if (!StringUtil.isEmpty(entity.getBillCode())) {
			querySql.append(" AND lol.bill_code like ?");
			sqlPageQuery.addLikeParam(entity.getBillCode());
		}
		
		if (!StringUtil.isEmpty(entity.getLoanDateStart())) {
			querySql.append(" AND lol.op_date >= ?");
			sqlPageQuery.addParam(entity.getLoanDateStart());
		}
		if (!StringUtil.isEmpty(entity.getLoanDateEnd())) {
			querySql.append(" AND lol.op_date < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(entity.getLoanDateEnd()), 1)));
		}
		
		querySql.append(" ORDER BY lol.op_date DESC ");
		
		sqlPageQuery.setQuerySql(querySql.toString());
		return loanapplyDao.findDataWithPage(sqlPageQuery);
	}
	
	
	/**
	 * 分页查询作废操作记录
	 * 
	 * @return
	 */
	public PageResult<Object[]> findByPage_ZF(O2oLoanSettledFormEntity entity,String opType) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 100, entity.getO2oIndex());
		StringBuffer querySql = new StringBuffer();
		
		querySql.append("  SELECT ol.loanapply_no,lol.bill_code,lol.op_psn,lol.settle_date,lol.remark  ");
		querySql.append("  FROM o2o_record_log lol  ");
		querySql.append("  LEFT JOIN o2o_loanapply ol ON lol.bill_code=ol.bill_code  ");
		querySql.append("  WHERE /**DATA_PERMISSION(ol.ltd)**/ 1=1  ");
		
		if (!StringUtil.isEmpty(opType)) {
			querySql.append(" AND lol.op_type = ?");
			sqlPageQuery.addParam(opType);
		}
		
		if (!StringUtil.isEmpty(entity.getBillCode())) {
			querySql.append(" AND lol.bill_code like ?");
			sqlPageQuery.addLikeParam(entity.getBillCode());
		}
		
		if (!StringUtil.isEmpty(entity.getLoanDateStart())) {
			querySql.append(" AND lol.op_date >= ?");
			sqlPageQuery.addParam(entity.getLoanDateStart());
		}
		if (!StringUtil.isEmpty(entity.getLoanDateEnd())) {
			querySql.append(" AND lol.op_date < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(entity.getLoanDateEnd()), 1)));
		}
		
		querySql.append(" ORDER BY lol.op_date DESC ");
		
		sqlPageQuery.setQuerySql(querySql.toString());
		return loanapplyDao.findDataWithPage(sqlPageQuery);
	}


  }
